package cn.szzsi.platform.controller.admin;

import cn.szzsi.AConfig;
import cn.szzsi.business.controller.admin.BaseAdminController;
import cn.szzsi.business.service.CompanyAuthLogService;
import cn.szzsi.business.service.CompanyInfoService;
import cn.szzsi.common.email.SendEmail;
import cn.szzsi.common.util.CommonUtil;
import cn.szzsi.model.CompanyAuthLog;
import cn.szzsi.model.CompanyInfo;
import cn.szzsi.model.Member;
import cn.szzsi.model.PlateformConfig;
import cn.szzsi.platform.interceptor.SysAdminInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.plugin.ehcache.CacheKit;
import org.apache.commons.codec.digest.DigestUtils;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2017/6/26.
 */
public class CompanyInfoController extends Controller{
    public void list(){
        String status=getPara("status");
        if("0".equals(status)){
            render(CommonUtil.TEMPLATE_PATH+"/platform/companyinfo/certification_temp_list.html");
        }else{
            render(CommonUtil.TEMPLATE_PATH+"/platform/companyinfo/certification_list.html");
        }

    }
    public void ajaxlist(){
        String status=getPara("status");
        String sql="select * from sys_company_info where 1=1";
        if(StrKit.notBlank(status)){
            sql+=" and status="+status+"";
        }
        if("2".equals(status)|| "0".equals(status)){
            sql+=" order by createDate desc";
        }else{
            sql+=" order by createDate";
        }

        List<CompanyInfo> lci = CompanyInfo.dao.find(sql);
        renderJson(lci);
    }

    /**
     * 企业详情
     */
    public void detail(){
        String id=getPara("id");
        CompanyInfo companyInfo=CompanyInfo.dao.findById(id);
        setAttr("companyInfo",companyInfo);
        render(CommonUtil.TEMPLATE_PATH+"/platform/companyinfo/certification_detail.html");
    }

    /**
     * 审核通过
     */
    @Before(Tx.class)
    public void hasverified(){
        String id=getPara("id");
        CompanyInfo companyInfo=CompanyInfo.dao.findById(id);
        companyInfo.setStatus("2");
        companyInfo.setTipsInfo("1");//审核后,商户第一次登陆会根据相应的提示
        companyInfo.update();
        CompanyAuthLogService.service.saveInfo(id,"审核","超级管理员","1","");

        //审核通过,商户密码设置为手机号
        Member member=Member.dao.findById(companyInfo.getShopId());
        member.setPassword(DigestUtils.md5Hex(companyInfo.getPhone()));
        member.update();
        //审核通过,商户密码设置为手机号

        renderJson(Ret.ok("info","操作成功!"));

    }

    /**
     * 审核不通过
     */
    public void noverified(){
        String id=getPara("id");
        String remark=getPara("remark");
        CompanyInfo companyInfo=CompanyInfo.dao.findById(id);
        companyInfo.setStatus("3");
        companyInfo.setTipsInfo("2");
        companyInfo.update();
        CompanyAuthLogService.service.saveInfo(id,"审核","超级管理员","0",remark);
        renderJson(Ret.ok("info","操作成功!"));
    }
}
